// main.js 
const { app, BrowserWindow ,screen ,ipcMain,Menu  } = require("electron");
const path = require("path");
const NODE_ENV = process.env.NODE_ENV;
const koa = require("./koa/index");
let  mainWindow;
 
Menu.setApplicationMenu(Menu.buildFromTemplate([]))

function createWindow() {
  let { width ,height } = screen.getPrimaryDisplay().workAreaSize 
  mainWindow = new BrowserWindow({ width: width, height: height ,  webPreferences:{
    // 定义预加载js
    preload:path.resolve(__dirname,'./preload.js'),
    webviewTag:true,
    webSecurity:false
}, });
  // win.loadURL(' http://localhost:5173/') 
  mainWindow.loadURL(app.isPackaged ? `file://${path.join(__dirname, "../dist/index.html") }`:"http://localhost:5173"); 
  mainWindow.webContents.openDevTools() 
} 

ipcMain.on('open', (e, data) => {
  console.log(data); // 打印出 {a: 1}
  mainWindow.webContents.openDevTools() 
});

app.whenReady().then(() => {
  koa.run()
  createWindow(); 
  app.on("activate", function () { 
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});
 
app.on("window-all-closed", function () {
  koa.stop()
  if (process.platform !== "darwin") app.quit();
}); 


/*
 store 
*/